package com.yima.baseproj.module.app.gentable.base;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class GeneratorUtil {

    public static void main(String[] args) throws IOException {
        GeneratorUtil.generatorZipMaps(
                "vm/xingzong/xingzong.cpp.vm",
                Arrays.asList(
                        MapUtil.<String,Object>builder()
                                .put("name","一码")
                                .put("fileName","test.cpp")
                                .build())
                ,"D:/YanWe/Desktop/xingzong/test.zip");
    }

    public static void generator(String template, Map<String,Object> map, String filePath) throws IOException {
        StringWriter sw = GeneratorUtil.xuanran(template,map);
        FileUtils.writeStringToFile(new File(filePath), sw.toString(), "UTF-8");
    }

    public static void generatorFile(String template, Map<String,Object> map, String folder) throws IOException {
        StringWriter sw = GeneratorUtil.xuanran(template,map);
        FileUtils.writeStringToFile(new File(getFilePath(folder,map)), sw.toString(), "UTF-8");
    }

    private static String getFilePath(String folder,Map<String,Object> map){
        String filePath = map.get("fileName").toString();
        if(StrUtil.isNotBlank(folder)){
            filePath = String.format("%s/%s",folder,map.get("fileName").toString());
        }else{
        }
        return filePath;
    }

    public static void generatorFileTemplates(List<String> templateList, Map<String,Object> map, String folder) throws IOException {
        for (String template : templateList) {
            generatorFile(template,map,folder);
        }
    }

    public static void generatorFileMaps(String template, List<Map<String,Object>> mapList, String folder) throws IOException {
        for (Map<String, Object> map : mapList) {
            generatorFile(template,map,folder);
        }
    }

    public static void generatorZipTemplates(List<String> templateList, Map<String,Object> map, String zipFilePath) throws IOException {
        // 添加到zip
        FileOutputStream outputStream = new FileOutputStream(zipFilePath);
        ZipOutputStream zip = new ZipOutputStream(outputStream, CharsetUtil.CHARSET_UTF_8);
        for (String template : templateList) {
            StringWriter sw = GeneratorUtil.xuanran(template,map);
            zip.putNextEntry(new ZipEntry(map.get("fileName").toString()));
            IOUtils.write(sw.toString(), zip, "UTF-8");
            sw.close();
        }

        zip.close();
    }

    public static void generatorZipMaps(String template, List<Map<String,Object>> mapList, String zipFilePath) throws IOException {
        // 添加到zip
        FileOutputStream outputStream = new FileOutputStream(zipFilePath);
        ZipOutputStream zip = new ZipOutputStream(outputStream, CharsetUtil.CHARSET_UTF_8);

        for (Map<String, Object> map : mapList) {
            StringWriter sw = GeneratorUtil.xuanran(template,map);
            zip.putNextEntry(new ZipEntry(map.get("fileName").toString()));
            IOUtils.write(sw.toString(), zip, "UTF-8");
            sw.close();
        }

        zip.close();
    }

    private static StringWriter xuanran(String template, Map<String,Object> map) {
        init();

        VelocityContext context = new VelocityContext(map);
        // 渲染模板
        StringWriter sw = new StringWriter();
        Template tpl = Velocity.getTemplate(template, "UTF-8");
        tpl.merge(context, sw);
        return sw;
    }

    /**
     * 初始化vm方法
     */
    private static void init() {
        Properties p = new Properties();
        try {
            // 加载classpath目录下的vm文件
            p.setProperty("resource.loader.file.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

            if(StrUtil.isNotBlank(GeneratorConfig.RESOURCE_ROOT_PATH)){
                p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, GeneratorConfig.RESOURCE_ROOT_PATH);
            }
            // 定义字符集
            p.setProperty(Velocity.INPUT_ENCODING, "UTF_8");
            // 初始化Velocity引擎，指定配置Properties
            Velocity.init(p);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
